package com.hopu.oa.controller.notice;

import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.notice.AoaNoticeList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.impl.notice.NoticeManageServiceImp;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class NoticeManageController {
    @Autowired
    private NoticeManageServiceImp noticeListService;

    @RequestMapping("infrommanage")
    public String infrommanage(@RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "size", defaultValue = "5") int size,
                               HttpSession session, Model model) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = noticeListService.queryNoticeListById(user.getUserId(), baseKey, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaNoticeList> noticeList = noticeListService.queryNoticeListById(user.getUserId(), baseKey, (page - 1) * size, size);

        HashMap<String, Object> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);


        model.addAttribute("page", map);
        model.addAttribute("list", noticeList);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "seinfrommanage");

        return "inform/informmanage";
    }


    @RequestMapping("seinfrommanage")
    public String seinfrommanage(@RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                 @RequestParam(value = "page", defaultValue = "1") int page,
                                 @RequestParam(value = "size", defaultValue = "5") int size,
                                 HttpSession session, Model model) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int totalElements = noticeListService.queryNoticeListById(user.getUserId(), baseKey, page, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaNoticeList> noticeList = noticeListService.queryNoticeListById(user.getUserId(), baseKey, (page - 1) * size, size);

        HashMap<String, Object> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);


        model.addAttribute("page", map);
        model.addAttribute("list", noticeList);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "seinfrommanage");

        return "inform/informtable";
    }

    /**
     * 进入修改添加页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("informedit")
    public String informedit(Integer id, Model model) {
        List<AoaStatusList> statusList = noticeListService.findStatusList();
        List<AoaTypeList> typeList = noticeListService.findTypeList();
        if (id == null) {
            model.addAttribute("typeList", typeList);
            model.addAttribute("statusList", statusList);
        } else {
            AoaNoticeList noticeList = noticeListService.queryNoticeList(id);
            model.addAttribute("typeList", typeList);
            model.addAttribute("statusList", statusList);
            model.addAttribute("noticeList", noticeList);
        }
        return "inform/informedit";
    }

    /**
     * 添加修改
     *
     * @return
     */
    @RequestMapping("informcheck")
    public String informcheck(AoaNoticeList noticeList, @Param("top") String top, Model model, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        noticeList.setNoticeTime(ft.format(date));
        noticeList.setModifyTime(ft.format(date));
        if ("on".equals(top)) {
            noticeList.setIsTop(1);
        } else {
            noticeList.setIsTop(0);
        }
        System.out.println(noticeList.getIsTop());
        if (noticeList.getNoticeId() == null) {
            System.out.println("111111111111111111111");//添加

            noticeList.setUserId(user.getUserId());
            int rid = noticeListService.addNoticeList(noticeList);

            /*
                转发通知
            */
            System.out.println("noticeList = " + noticeList);

//            boolean b = noticeListService.forwardInform(noticeListService.queryNoticeList(noticeList.getNoticeId()), user);
            boolean b = noticeListService.forwardInform(noticeList, user);

            System.out.println("bbbbbbbbbbbbbbbbb=" + b);


        } else {
            noticeList.setUserId(user.getUserId());
            noticeList.setModifyTime(ft.format(date));
            noticeListService.updateNoticeList(noticeList);
        }
        return "redirect:infrommanage";
    }

    /**
     * 进入查看页面
     *
     * @param id
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("informshow")
    public String informshow(Integer id, Integer read, Model model, HttpSession session, Integer relationid) {
        AoaNoticeList noticeList = noticeListService.queryNoticeList(relationid);
        model.addAttribute("notice", noticeList);
        AoaUser user = (AoaUser) session.getAttribute("user");
        String username = user.getUserName();


        boolean b = noticeListService.setNoticeUserByUserIdAndNoticeId(user.getUserId(), relationid);

        noticeListService.forwardInform(noticeListService.queryNoticeList(relationid), user);
        model.addAttribute("userName", username);
        return "inform/informshow";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("infromdelete")
    public String infromdelete(Integer id, Integer page, Integer list) {
        noticeListService.deleteNoticeUser(id);
        noticeListService.deleteNoticeList(id);
        if (list != null) {
            return "redirect:infromlist";
        } else {
            return "redirect:infrommanage";
        }
    }
}
